package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.JournalArticlesMapper;
import com.zhijian.medical.entity.po.JournalArticles;
import com.zhijian.medical.entity.vo.request.JournalArticlesSearchVo;
import com.zhijian.medical.entity.vo.request.JournalArticlesSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.JournalArticlesVo;
import com.zhijian.medical.service.JournalArticlesService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 期刊论文业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-04-29 17:19:20
 */
@Slf4j
@Service
public class JournalArticlesServiceImpl implements JournalArticlesService {

	@Autowired
    private JournalArticlesMapper mapper;

    @Override
    public JournalArticles selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public JournalArticlesVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(JournalArticles journalArticles) {
        Date now = new Date();
        journalArticles.setCreateTime(now);
        journalArticles.setUpdateTime(now);
        return mapper.insert(journalArticles) > 0 ? journalArticles.getId() : 0L;
    }

    @Override
    public boolean update(JournalArticles journalArticles) {
        Date now = new Date();
        journalArticles.setUpdateTime(now);
        return mapper.updateByPrimaryKey(journalArticles) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(JournalArticles record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(JournalArticlesSaveOrUpdateVo saveOrUpdateVo) {
        JournalArticles journalArticles = new JournalArticles();
        BeanUtils.copyProperties(saveOrUpdateVo, journalArticles);
        return this.insert(journalArticles) != 0;
    }

    @Override
    public boolean modify(JournalArticlesSaveOrUpdateVo saveOrUpdateVo) {
        JournalArticles old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        JournalArticles journalArticles = new JournalArticles();
        BeanUtils.copyProperties(saveOrUpdateVo, journalArticles);
        return this.update(journalArticles);
    }

    @Override
    public JournalArticlesVo selectOne(JournalArticlesSearchVo journalArticlesSearchVo) {
        return mapper.selectOne(journalArticlesSearchVo);
     }

    @Override
    public Integer count(JournalArticlesSearchVo journalArticlesSearchVo) {
        return mapper.count(journalArticlesSearchVo);
    }

    @Override
    public List<JournalArticlesVo> selectList(JournalArticlesSearchVo journalArticlesSearchVo) {
        return mapper.selectList(journalArticlesSearchVo);
    }

    @Override
    public Page<JournalArticlesVo> selectPage(JournalArticlesSearchVo journalArticlesSearchVo) {
        AssertUtil.check(Objects.isNull(journalArticlesSearchVo.getPageNum()) || Objects.isNull(journalArticlesSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(journalArticlesSearchVo);
        //分页
        Page<JournalArticlesVo> page = new Page<>(true, count, journalArticlesSearchVo.getPageNum(), journalArticlesSearchVo.getPageSize());
        journalArticlesSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<JournalArticlesVo> responseVos = this.selectList(journalArticlesSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<JournalArticles> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(JournalArticlesSearchVo journalArticlesSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(journalArticlesSearchVo);
    }
    
}